package com.newx.file.template.impl;

import cn.hutool.core.lang.Assert;
import com.newx.file.config.NewxFileConfig;
import com.newx.file.handler.NewxFileException;
import com.newx.file.param.AliyunFileUploadParam;
import com.newx.file.template.AbstractFileApiTemplate;
import com.newx.file.util.FileUploadUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author yxl
 * @date 2024/7/22 0022 15:14
 */
@Service
@Slf4j
@AllArgsConstructor
public class AliyunFileApiTemplateImpl extends AbstractFileApiTemplate {

    private NewxFileConfig newxFileConfig;
    @Override
    public Object uploadFile(MultipartFile file) {
        Assert.notNull(newxFileConfig.getAliyunAccessKeyId(), "阿里云accessKeyId不能为空");
        Assert.notNull(newxFileConfig.getAliyunAccessKeySecret(), "阿里云accessKeySecret不能为空");
        Assert.notNull(newxFileConfig.getAliyunBucket(), "阿里云bucket不能为空");
        Assert.notNull(newxFileConfig.getAliyunDomain(), "阿里云domain不能为空");
        Assert.notNull(newxFileConfig.getAliyunEndpoint(), "阿里云endpoint不能为空");
        Assert.notNull(newxFileConfig.getAliyunPackagePath(), "阿里云packagePath不能为空");
        AliyunFileUploadParam param = new AliyunFileUploadParam();
        param.setFile(file)
                .setAccessKeyId(newxFileConfig.getAliyunAccessKeyId())
                .setAccessKeySecret(newxFileConfig.getAliyunAccessKeySecret())
                .setBucket(newxFileConfig.getAliyunBucket())
                .setDomain(newxFileConfig.getAliyunDomain())
                .setEndpoint(newxFileConfig.getAliyunEndpoint())
                .setPackagePath(newxFileConfig.getAliyunPackagePath());
        return FileUploadUtil.uploadAliyunFile(param);
    }
}
